package org.jin.samples.dao.impl;

import java.util.ArrayList;

import java.util.List;

import org.jin.samples.dao.IUserSubjectDAO;
import org.jin.samples.model.ExamUserSubject;
import org.jin.samples.page.PagerModel;
import org.jin.samples.page.SystemContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class UserSubjectDAOimpl extends BaseHibernateDAO<ExamUserSubject> implements IUserSubjectDAO{
	

	
	@Override
	public List<ExamUserSubject> findUserSubject(){
		String hql = "from ExamUserSubject";
		return this.find(hql);
	}
	
	@Override
	public List<ExamUserSubject> findAllSubjectByUserId(String id)throws Exception{
		String hql="from ExamUserSubject where user_id="+id;
		return this.find(hql);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public PagerModel<ExamUserSubject> findAllSubjectByUserIdPage(String id){
		PagerModel<ExamUserSubject> page = new  PagerModel<ExamUserSubject>();
		List<ExamUserSubject> dates = new ArrayList<ExamUserSubject>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPagesize();
		List<ExamUserSubject> tempDates = this.getSession().createQuery(" from ExamUserSubject where user_id = "+id).setFirstResult(offset).setMaxResults(pageSize).list();
		for(ExamUserSubject userSubject : tempDates){
			dates.add(userSubject);
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		//获取总记录数
		long totalSize = (long) this.getSession().createQuery("select count(*) from ExamUserSubject where user_id = "+id).uniqueResult();
		page.setTotalSize(totalSize);
		return page;	
	}
	
}
